<?php

namespace app\controller\admin\fire;

use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\Cache;

/**
 * Class Record
 * @package app\controller\admin\peace
 */
class Record extends Backend
{
    /**
     * api_log模型对象
     * @var \app\model\api\Log
     */
    protected $model;
    protected $hasSoftDel=0;//是否拥有软删除功能

    public $orderRule = ['sort' => 'desc', 'id' => 'asc'];

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法

    public function _initialize()
    {
        $this->model = new \app\model\api\fire\FireRecord();
    }


    //查看
    public function index(){
        $where = $this->buildSearchParams();
        $order = $this->buildOrder();
        $allData = $this->request->param('all_data');
        $data = $this->model->where($where)->where(array('type' => 1))->order($order)->with(['author']);
        if($allData){
            $data = $data->select()->toArray();
        }else{
            $limit = $this->request->param('limit', 10);
            $data = $data->paginate($limit)->toArray();
        }
        return $this->success('数据获取成功', $data);
    }


    //添加
    public function add()
    {
        $post     = CommonFun::filterPostData($this->request->post());
        $insData = array();
        $hand = array();
        $gyroscope = array();

        foreach($post as $k => $v){
            if(empty($v)){
                continue;
            }
            if(strpos($k,'-') !== false){
                $arr = explode('-',$k);
                ${$arr[0]}[] = $v;
            }else{
                $insData[$k] = $v;
            }
        }
        $insData['hand']      = json_encode($hand,JSON_UNESCAPED_UNICODE);
        $insData['gyroscope'] = json_encode($gyroscope,JSON_UNESCAPED_UNICODE);;
        $insData['create_time'] = time();

        try {
            $save = $this->model->save($insData);
            if (!$save) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $post = CommonFun::filterPostData($this->request->post());
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');

        $hand = array();
        $gyroscope = array();

        foreach($post as $k => $v){
            if(empty($v)){
                continue;
            }
            if(strpos($k,'-') !== false){
                $arr = explode('-',$k);
                ${$arr[0]}[] = $v;
            }else{
                $row->$k = $v;
            }
        }

        $row->hand = json_encode($hand,JSON_UNESCAPED_UNICODE);
        $row->gyroscope = json_encode($gyroscope,JSON_UNESCAPED_UNICODE);
        try {
            $save = $row->save();
            if ($save === false) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }


    //查看详情
    public function info()
    {
        $id   = $this->request->param('id');
        $data = $this->model->findOrEmpty($id)->toArray();

        $data['hand'] = json_decode($data['hand'],true);
        $data['gyroscope'] = json_decode($data['gyroscope'],true);

        return $this->success('获取成功', $data);
    }

}